package com.gbx.ximalayating.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gbx.ximalayating.R;
import com.gbx.ximalayating.activity.YouLikeActivity;
import com.gbx.ximalayating.adapter.DiscoveryColumnsAdapter;
import com.gbx.ximalayating.adapter.FocusImageAdapter;
import com.gbx.ximalayating.adapter.HotRecommendsAdapter;
import com.gbx.ximalayating.application.BaseApplication;
import com.gbx.ximalayating.entity.RecommendOne;
import com.gbx.ximalayating.entity.RecommendTwo;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecommendFragment extends Fragment {

    private FocusImageAdapter focusAdapter;
    private ViewPager focuspager;
    private RecyclerView recycler;
    private ViewPager discoverpager;
    private TextView guesstitle;
    private HotRecommendsAdapter hotRecommendsAdapter;
    private ImageView guessImg1,guessImg2,guessImg3,guessImg4,guessImg5,guessImg6;
    private TextView guessRes1,guessRes2,guessRes3,guessRes4,guessRes5,guessRes6;
    private TextView guessSum1,guessSum2,guessSum3,guessSum4,guessSum5,guessSum6;
    private View.OnClickListener listener;


    public RecommendFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_recommend, container, false);
    }

    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        discoverpager = ((ViewPager) view.findViewById(R.id.discoveryColumns_pager));
        focuspager = (ViewPager) view.findViewById(R.id.fcusImages_pager);
        recycler = ((RecyclerView) view.findViewById(R.id.recycler));
        guesstitle = ((TextView) view.findViewById(R.id.guesstext));
        guessImg1 = ((ImageView) view.findViewById(R.id.guessimage_1));
        guessImg2 = ((ImageView) view.findViewById(R.id.guessimage_2));
        guessImg3 = ((ImageView) view.findViewById(R.id.guessimage_3));
        guessImg4 = ((ImageView) view.findViewById(R.id.guessimage_4));
        guessImg5 = ((ImageView) view.findViewById(R.id.guessimage_5));
        guessImg6 = ((ImageView) view.findViewById(R.id.guessimage_6));
        guessRes1 = ((TextView) view.findViewById(R.id.guessres_1));
        guessRes2 = ((TextView) view.findViewById(R.id.guessres_2));
        guessRes3 = ((TextView) view.findViewById(R.id.guessres_3));
        guessRes4 = ((TextView) view.findViewById(R.id.guessres_4));
        guessRes5 = ((TextView) view.findViewById(R.id.guessres_5));
        guessRes6 = ((TextView) view.findViewById(R.id.guessres_6));
        guessSum1 = ((TextView) view.findViewById(R.id.guesssum_1));
        guessSum2 = ((TextView) view.findViewById(R.id.guesssum_2));
        guessSum3 = ((TextView) view.findViewById(R.id.guesssum_3));
        guessSum4 = ((TextView) view.findViewById(R.id.guesssum_4));
        guessSum5 = ((TextView) view.findViewById(R.id.guesssum_5));
        guessSum6 = ((TextView) view.findViewById(R.id.guesssum_6));

        listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), YouLikeActivity.class);
                startActivity(intent);
            }
        };
        guessImg1.setOnClickListener(listener);
        guessImg2.setOnClickListener(listener);
        guessImg3.setOnClickListener(listener);
        guessImg4.setOnClickListener(listener);
        guessImg5.setOnClickListener(listener);
        guessImg6.setOnClickListener(listener);


        BaseApplication application = (BaseApplication) getActivity().getApplication();
        application.getRecomOne().getRecomOne().enqueue(new Callback<RecommendOne>() {
            private DiscoveryColumnsAdapter discoverAdapter;

            @Override
            public void onResponse(Call<RecommendOne> call, Response<RecommendOne> response) {
                List<RecommendOne.FocusImages.ConEntity> list = response.body().getFocusImages().getList();
                List<RecommendOne.DiscoveryColumns.ConEntity> disList = response.body().getDiscoveryColumns().getList();
                focusAdapter = new FocusImageAdapter(getActivity().getSupportFragmentManager(), list);
                focuspager.setAdapter(focusAdapter);
                discoverAdapter = new DiscoveryColumnsAdapter(getActivity().getSupportFragmentManager(),disList);
                discoverpager.setAdapter(discoverAdapter);

            }
            @Override
            public void onFailure(Call<RecommendOne> call, Throwable t) {
                t.printStackTrace();
                Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        application.getRecomTwo().getRecomTwo().enqueue(new Callback<RecommendTwo>() {
            @Override
            public void onResponse(Call<RecommendTwo> call, Response<RecommendTwo> response) {
                List<RecommendTwo.HotRecommends.ConEntity> list = response.body().getHotRecommends().getList();
                List<RecommendTwo.Guess.ConEntity> guessList = response.body().getGuess().getList();
                String guessTilte = response.body().getGuess().getTitle();
                guesstitle.setText(guessTilte);
                Picasso.with(getContext()).load(guessList.get(0).getCoverLarge()).into(guessImg1);
                Picasso.with(getContext()).load(guessList.get(1).getCoverLarge()).into(guessImg2);
                Picasso.with(getContext()).load(guessList.get(2).getCoverLarge()).into(guessImg3);
                Picasso.with(getContext()).load(guessList.get(3).getCoverLarge()).into(guessImg4);
                Picasso.with(getContext()).load(guessList.get(4).getCoverLarge()).into(guessImg5);
                Picasso.with(getContext()).load(guessList.get(5).getCoverLarge()).into(guessImg6);
                guessSum1.setText(guessList.get(0).getTitle());
                guessSum2.setText(guessList.get(1).getTitle());
                guessSum3.setText(guessList.get(2).getTitle());
                guessSum4.setText(guessList.get(3).getTitle());
                guessSum5.setText(guessList.get(4).getTitle());
                guessSum6.setText(guessList.get(5).getTitle());
                guessRes1.setText(guessList.get(0).getNickname());
                guessRes2.setText(guessList.get(1).getNickname());
                guessRes3.setText(guessList.get(2).getNickname());
                guessRes4.setText(guessList.get(3).getNickname());
                guessRes5.setText(guessList.get(4).getNickname());
                guessRes6.setText(guessList.get(5).getNickname());
                hotRecommendsAdapter = new HotRecommendsAdapter(getContext(), list);
                recycler.setAdapter(hotRecommendsAdapter);
                hotRecommendsAdapter.setOnItemClickListener(new HotRecommendsAdapter.OnRecyclerViewItemClickListener() {
                    @Override
                    public void onItemClick(View view) {
                        Intent intent = new Intent(getActivity(), YouLikeActivity.class);
                        startActivity(intent);
                    }
                });
                View footview = LayoutInflater.from(getContext()).inflate(R.layout.fragment_recom_foot, null);
                hotRecommendsAdapter.addFooterView(footview);
                RelativeLayout relative = (RelativeLayout) footview.findViewById(R.id.relative);
                relative.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ViewPager viewPager = (ViewPager) getActivity().findViewById(R.id.find_pager);
                        viewPager.setCurrentItem(1);

                    }
                });
            }

            @Override
            public void onFailure(Call<RecommendTwo> call, Throwable t) {
                t.printStackTrace();
                Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
